package com.yonyou.gmmc.service.violationInquiry.service;

import java.util.Date;
import java.util.Map;

import org.marketing.common.util.CheckUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.violationInquiry.constant.ViolationInquiryConstant;
import com.yonyou.gmmc.service.violationInquiry.dao.TmRegulationsHttpMapper;
import com.yonyou.gmmc.service.violationInquiry.dao.TmRegulationsThridMapper;
import com.yonyou.gmmc.service.violationInquiry.model.TmRegulationsHttp;
import com.yonyou.gmmc.service.violationInquiry.model.TmRegulationsThrid;

import net.sf.json.JSONObject;

@Service
@Transactional(propagation=Propagation.REQUIRES_NEW)
public class SaveThridService {
	private static Logger logger = LoggerFactory.getLogger(SaveThridService.class);
	@Autowired
	private TmRegulationsThridMapper tmRegulationsThridMapper;
	@Autowired
	private TmRegulationsHttpMapper tmRegulationsHttpMapper;
	public void saveThrid(Map<String, String> map) {
		String vin = map.get("vin");
		String registNo = map.get("registNo");
		String engine = map.get("engine");
		String userId = map.get("userId");
		String city = map.get("city");
		// 保存查询历史
		TmRegulationsThrid thrid = new TmRegulationsThrid();
		thrid.setUserId(Integer.valueOf(userId));
		thrid.setRegistNo(registNo);
		if (!CheckUtil.NullOrEmpty(city)) {
			thrid.setCityCode(city);
		}
		if (!CheckUtil.NullOrEmpty(vin)) {
			thrid.setVin(vin);
		}
		if (!CheckUtil.NullOrEmpty(engine)) {
			thrid.setEngine(engine);
		}
		// 1正常 0删除
		thrid.setResCode((short) 1);
		thrid.setCreateDate(new Date());
		thrid.setCreateBy(ViolationInquiryConstant.OPT_USER);
		tmRegulationsThridMapper.insertSelective(thrid);
	}
	
	public void saveRegulationsHttp(Map<String, String> map,String response) {
		logger.info("SaveThridService.saveThrid==>registNo:" + map.get("registNo") + "   vin:"
				+ map.get("vin") + "   engine:" + map.get("engine")+ "   _userId:" + map.get("_userId"));
		String vin = map.get("vin");
		String registNo = map.get("registNo");
		String engine = map.get("engine");
		String userId = map.get("userId");
		String reg_id = map.get("reg_id");
		// 保存查询历史
		TmRegulationsHttp h = new TmRegulationsHttp();
		h.setUserId(Integer.valueOf(userId));
		h.setRegistno(registNo);
		h.setRegId(reg_id);
		if (vin != null && !"".equals(vin)) {
			h.setVin(vin);
		}
		if (engine != null && !"".equals(engine)) {
			h.setEngine(engine);
		}
		JSONObject node = JSONObject.fromObject(response);
		String ErrorCode = node.getString("ErrorCode");
		String ErrMessage = node.getString("ErrMessage");
		String HasData = node.getString("HasData");
		String Success = node.getString("Success");
		String ResultType = node.getString("ResultType");
		String LastSearchTime = node.getString("LastSearchTime");
		String Other = node.getString("Other");
		h.setErrorcode(ErrorCode);
		h.setErrmessage(ErrMessage);
		h.setHasdata(HasData);
		h.setSuccess(Success);
		h.setResulttype(ResultType);
		h.setLastsearchtime(LastSearchTime);
		h.setOther(Other);
		h.setCreateDate(new Date());
		h.setCreateBy(ViolationInquiryConstant.OPT_USER);
		tmRegulationsHttpMapper.insertSelective(h);
	}
}
